<?php
	include_once("config.php");
	
	function process_json($text) {
		//$text = stripslashes($text);
		$text = str_replace("\n", ' ', $text);
		//$text = str_replace('"', "'", $text);
		$text = str_replace("\r", ' ', $text);
		return $text; 
	}
	
	// step 1: gather the information and assemble a MySQL query
	// TO-DO: Inject server-side session code to detect user's login
	//$username = "temp";
	session_start();
	$username = $HTTP_SESSION_VARS['username']; 
	
	$sql = "select playlist, id, username from ajax_video";
	db_connect();
	$result = exec_results($sql);
	
	// step 2: compile the json playlist information
	$json_text = "[";
	
	$row = mysql_fetch_assoc($result);
	if (mysql_num_rows($result) > 0) {
		$current_entry = "{ name: '".$row['username'].":".$row['playlist']."', id: ".$row['id']."}";
		$json_text = $json_text.$current_entry;
		while ($row = mysql_fetch_assoc($result)) {
			$current_entry = ",{ name: '".$row['username'].":".$row['playlist']."', id: ".$row['id']."}";
			$json_text = $json_text.$current_entry;
		}
	
		$json_text = $json_text."]";
		echo $json_text; 
	} else {
		echo "[]"; 
	}
?>